<template>
  <div>
    <div class="list">
      <scroll-view
        class="scroll-wrap no-scroll-bar"
        style="height:calc(100% - 10px)"
        scroll-y
        @scrolltolower="handleLoadMore"
      >
        <div class="content" v-for="(item,index) in userList" :key="index">
          <div class="left">
            <img :src="item.avatar" alt mode="scaleToFill">
            <div>
              <p>{{item.nickname}}</p>
              <span>{{item.phone}}</span>
            </div>
          </div>
          <div class="right">
            <button @click="deleteUser(item)">删除</button>
          </div>
        </div>
        <p v-if="alreadyLastPage" class="end-tip">没有更多了</p>
      </scroll-view>
    </div>
    <button class="btn" @click="addVirtualUser">添加</button>
  </div>
</template>

<script>
import { FIND_CUSTOMER_VIRTUAL_PAGE, DELETE_CUSTOMER_VIRTUAL } from '@/api'

export default {
  components: {},
  data () {
    return {
      userList: [],
      alreadyLastPage: false,
      currentPage: 1
    }
  },
  computed: {
    account () {
      return this.$cookie.get('account')
    }
  },
  onShow () {
    this.currentPage = 1
    this._findUserList()
  },
  created () {},
  methods: {
    handleLoadMore () {
      if (!this.alreadyLastPage) {
        this._findUserList()
      }
    },
    deleteUser (item) {
      const vm = this
      vm.$dialog('是否删除虚拟用户“' + item.nickname + '”').then(r => {
        vm._postDeleteUser(item)
      })
    },
    addVirtualUser () {
      this.$router.push('/pages/home/group/virtual-add-user/main')
    },
    _findUserList () {
      const vm = this
      FIND_CUSTOMER_VIRTUAL_PAGE(vm.account.shopId, vm.currentPage, 10)
        .then(r => {
          if (r.data.code === '0') {
            let reg = /:/g
            let { isFirstPage, isLastPage } = r.data.data
            let list = r.data.data.list
            list.map(_ => {
              _.avatar = _.avatar.replace(reg, 's' + ':')
            })
            vm.printList(list, isFirstPage, isLastPage)
          } else {
            vm.$toast(r.data.message)
          }
        })
        .catch(e => {
          console.info(e)
        })
    },
    printList (list, isFirst, isLast) {
      const vm = this
      if (isFirst) {
        vm.userList = list
        if (isLast || !list.length) {
          vm.alreadyLastPage = true
        } else {
          vm.alreadyLastPage = false
          vm.currentPage += 1
        }
      } else if (isLast) {
        vm.userList.push(...list)
        vm.alreadyLastPage = true
      } else {
        vm.userList.push(...list)
        vm.alreadyLastPage = false
        vm.currentPage += 1
      }
    },
    _postDeleteUser (item) {
      const vm = this
      DELETE_CUSTOMER_VIRTUAL(item.shopId, item.id)
        .then(r => {
          if (r.data.code === '0') {
            vm.$alert('删除成功', '').then(() => {
              this.currentPage = 1
              this._findUserList()
            })
          } else {
            vm.$toast(r.data.message)
          }
        })
        .catch(e => {
          console.info(e)
        })
    }
  }
}
</script>

<style lang="scss">
@import 'static/styles/color.scss';

body {
  background: #edf0f5;
}
.list {
  height: calc(100% - 46px);
  > .scroll-wrap {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 80px;
    position: relative;
    overflow: hidden;
    transform: translateY(10px);
    .content {
      width: 100%;
      height: 60px;
      background: #fff;
      margin-bottom: 4px;
      display: flex;
      justify-content: space-between;
      .left {
        display: flex;
        align-items: center;
        img {
          width: 40px;
          height: 40px;
          border-radius: 50px;
          margin: 0 20px;
        }
        div {
          display: flex;
          flex-direction: column;
          justify-content: center;
          font-size: 14px;
          color: #000;
          span {
            color: #9a9a9a;
          }
        }
      }
      .right {
        display: flex;
        align-items: center;
        margin-right: 20px;
        button {
          width: 60px;
          height: 20px;
          font-size: 12px;
          color: #fff;
          background: #ff371c;
          border-radius: 5px;
          line-height: 20px;
        }
      }
    }
  }

  .end-tip {
    margin: 0;
    padding: 15px;
    text-align: center;
    color: $gray;
    font-size: 15px;
  }
}
.btn {
  width: 100%;
  height: 45px;
  background: #5d93fd;
  font-size: 16px;
  color: #fff;
  line-height: 45px;
  position: fixed;
  bottom: 0;
  border-radius: 0;
}
</style>

